package com.dartfar.organize.service;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.type.IntegerType;
import org.hibernate.type.Type;
import org.springframework.stereotype.Component;

import com.dartfar.base.dao.DaoUtil;
import com.dartfar.base.service.BaseServiceImpl;
import com.dartfar.organize.model.Department;

@Component("departService")
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {
	
	private DaoUtil<Department> departDaoUtil;


	
	
	/**
	 * 根据父ID获取部门列表
	 * @param parentId 父ID
	 * @return 返回部门列表
	 */
	@Override
	public List<Department> getListByParentId(int parentId) {
		if(parentId == 0){
			return departDaoUtil.getList("from Department d where d.parentId is null");
		}else{
			return departDaoUtil.getList("from Department d where d.parentId = ?",new Object[]{parentId},new Type[]{new IntegerType()});
		}
	}	

	/**
	 * 根据父ID获取部门列表
	 * @param parentId 父ID
	 * @return 返回部门列表
	 */
	@Override
	public List<Department> getList() {
		return departDaoUtil.getList("from Department");
	}
	
	
	/**
	 * 获取部门下的第一个子部门
	 * @param groupId 部门ID
	 * @return 子部门
	 */
	@Override
	public Department getFirst(int departId) {
		List<Department> departList = departDaoUtil.getListForPage("from Department d where d.parentId=?", new Object[]{departId}, new Type[]{new IntegerType()},0,1);
		if(departList.size() != 0){
			return departList.get(0);
		}else{
			return null;
		}
	}
	
	/**
	 * 根据部门代码获取部门名称
	 * @param groupId 部门ID
	 * @return 子部门
	 */
	@Override
	public Department getDepartmentByCode(String code) {
		String hql = "from Department d where d.code = '" + code + "'";
		return departDaoUtil.getList(hql).get(0);
	}
	
	/**
	 * 注入
	 */
	@Resource
	public void setDepartDaoUtil(DaoUtil<Department> departDaoUtil) {
		this.departDaoUtil = departDaoUtil;
	}


}
